//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Ext.h".
//-------------------------------------------------------------------

#ifndef _UXS_POSIX_TIMER_H
#define _UXS_POSIX_TIMER_H

#pragma once

#include "OSPosix.h"
#include "../Base/Date.h"

#ifdef PLATFORM_BASE_POSIX

namespace UX_STL
{
namespace _platform_
{

#	ifdef PLATFORM_BASE_POSIX_SHELL

	//
	// Perfomance Timer
	//
	
	struct PerfTimer
	{
		// seconds
		double GetTime () const
		{
			timeval tv;
			gettimeofday( &tv, null );
			return tv.tv_sec + tv.tv_usec * 1.0e-6;
		}

		// milliseconds
		uint64 GetTimeMilliSec () const
		{
			timeval tv;
			gettimeofday( &tv, null );
			return uint64(tv.tv_sec) * 1000 + tv.tv_usec / 1000;
		}

		// microseconds
		uint64 GetTimeMicroSec () const
		{
			timeval tv;
			gettimeofday( &tv, null );
			return uint64(tv.tv_sec) * 1000000 + tv.tv_usec;
		}
	};

#	endif	// PLATFORM_BASE_POSIX_SHELL

	

	// Now
	inline Date & Date::Now ()
	{
		struct timeval  tv;
		gettimeofday( &tv, 0 );

		time_t t = tv.tv_sec;
		struct tm * dt = localtime( & t );

		year			= dt->tm_year + 1900;
		month			= dt->tm_mon + 1;							// 0..11
		day				= dt->tm_mday;								// 1..31
		dayOfWeek		= dt->tm_wday == 0 ? 6 : dt->tm_wday-1;		// 0..6, Sun == 0
		dayOfYear		= dt->tm_yday + 1;							// 0..365

		hour			= dt->tm_hour;								// 0..23
		minute			= dt->tm_min;								// 0..59
		second			= dt->tm_sec;								// 0..59
		milliseconds	= tv.tv_usec / 1000;						// 0..1000

		return *this;
	}

//-------------------------------------------------------------------

}	// _platform_
}	// UX_STL

#endif	// PLATFORM_BASE_POSIX
#endif	// _UXS_POSIX_TIMER_H